package com.company.project;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Scanner;

//演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {

	 /**
	  * 读取控制台内容
	  */
	 public static String scanner(String tip) {
		 Scanner scanner = new Scanner(System.in);
	    
	     StringBuilder help = new StringBuilder();
	     help.append("请输入" + tip + "：");
	     System.out.println(help.toString());
	     if (scanner.hasNext()) {
	         String ipt = scanner.next();
	         if (StringUtils.isNotEmpty(ipt)) {
	             return ipt;
	         }
	     }
	     throw new MybatisPlusException("请输入正确的" + tip + "！");
	 }
	
	 public static void main(String[] args) {
         // 自定义参数
         String parent = "com.company.project.web";
         String moduleName = "finance";
         String table = "finance_bill_serviceitem";
         String tablePrefix = "finance_";
         String entity = "pojo." + table.split(tablePrefix)[1];

	     // 代码生成器
	     AutoGenerator autoGenerator = new AutoGenerator();
	
	     // 全局配置
	     GlobalConfig globalConfig = new GlobalConfig();
         globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
         globalConfig.setAuthor("任建波");
         globalConfig.setEnableCache(false);// XML 二级缓存
         globalConfig.setOpen(false);   // //是否打开输出目录 默认true
         globalConfig.setFileOverride(false); // 指定文件覆盖
         globalConfig.setBaseColumnList(false); // 基本的sql片段
         globalConfig.setBaseResultMap(false); // 基本的字段映射
         globalConfig.setServiceName("%sService");

	     // 数据源配置
	     DataSourceConfig dsc = new DataSourceConfig();
	     dsc.setUrl("jdbc:mysql://121.40.137.159:3306/guanyuan?useUnicode=true&useSSL=false&characterEncoding=utf8");
	     dsc.setDriverName("com.mysql.jdbc.Driver");
	     dsc.setUsername("root");
	     dsc.setPassword("root");

	     // 包配置
	     PackageConfig packageConfig = new PackageConfig();
//         packageConfig.setModuleName(scanner("模块名"));
         packageConfig.setModuleName(moduleName);
         packageConfig.setParent(parent);
         packageConfig.setEntity(entity);

	     // 策略配置
	     StrategyConfig strategy = new StrategyConfig();
	     strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
	     strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
         strategy.setEntityLombokModel(false);//【实体】是否为lombok模型（默认 false）
         strategy.setRestControllerStyle(false);//生成 @RestController 控制器
//	     strategy.setInclude(scanner("user,payorder").split(","));
//	     strategy.setInclude(scanner("sys_handcard"));
	     strategy.setInclude(table);    // 表
	     strategy.setControllerMappingHyphenStyle(true);//驼峰转连字符
//	     strategy.setTablePrefix(packageConfig.getModuleName() + "_");//表前缀
	     strategy.setTablePrefix(tablePrefix);//表前缀

         // 执行
         autoGenerator.setGlobalConfig(globalConfig);
         autoGenerator.setDataSource(dsc);
         autoGenerator.setPackageInfo(packageConfig);
         autoGenerator.setStrategy(strategy);
         autoGenerator.execute();
	 }

}